package com.li.network.lab_3;

import java.io.*;
import java.net.Socket;
import java.util.Base64;

public class MailClient {

    public static void main(String[] args) {
        String serverHost = "smtp.qq.com";
        int serverPort = 25;
        String from = "1914895959@qq.com";
        String to = "1359539470@qq.com";
        String key = "gdhspgvsijlzbdje";

        try (Socket client = new Socket(serverHost, serverPort)) {
            //连接QQ邮箱服务器
            BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
            Parser.getMessage(reader);
            PrintWriter writer = new PrintWriter(client.getOutputStream(), true);
            //hello
            writer.println("HELO liyongqi");
            Parser.getMessage(reader);
            //发起登录请求
            writer.println("AUTH LOGIN");
            Parser.getMessage(reader);
            //输入邮箱账号
            writer.println(new String(Base64.getEncoder().encode(from.getBytes())));
            Parser.getMessage(reader);
            //输入邮箱密码
            writer.println(new String(Base64.getEncoder().encode(key.getBytes())));
            Parser.getMessage(reader);
            writer.println("MAIL FROM: <" + from + ">");
            Parser.getMessage(reader);
            writer.println("RCPT TO: <" + to + ">");
            Parser.getMessage(reader);
            writer.println("DATA");
            Parser.getMessage(reader);
            writer.println("SUBJECT: hello, network");
            Parser.getMessage(reader);
            writer.println("FROM: " + from);
            writer.println("TO: " + to);
            writer.println("Content-Type:text/html");
            writer.println();
            writer.println("I love network~");
            writer.println(".");
            Parser.getMessage(reader);
            writer.println("QUIT");
            Parser.getMessage(reader);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}